package cn.well.cloud.modular.controller;

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import cn.well.cloud.base.auth.annotion.Permission;
import cn.well.cloud.base.enums.WXTypeEnums;
import cn.well.cloud.base.pojo.page.LayuiPageFactory;
import cn.well.cloud.modular.model.CourseInfo;
import cn.well.cloud.modular.service.ICourseInfoService;
import cn.well.cloud.sys.core.exception.enums.BizExceptionEnum;
import cn.well.cloud.sys.core.log.LogObjectHolder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 直播课程控制器
 *
 * @author jiawei
 * @Date 2020-05-26 20:13:33
 */
@Controller
@RequestMapping("/courseInfo")
public class CourseInfoController extends BaseController {

    private String PREFIX = "/modular/course/courseInfo/";

    @Autowired
    private ICourseInfoService courseInfoService;

    /**
     * 跳转到直播课程首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "courseInfo.html";
    }

    /**
     * 跳转到添加直播课程
     */
    @RequestMapping("/courseInfo_add")
    public String courseInfoAdd() {
        return PREFIX + "courseInfo_add.html";
    }

    /**
     * 跳转到编辑直播课程
     */
    @RequestMapping("/courseInfo_edit")
    public String courseInfoEdit(@RequestParam Long courseInfoId) {
		if (ToolUtil.isEmpty(courseInfoId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        CourseInfo courseInfo = courseInfoService.getById(courseInfoId);
        LogObjectHolder.me().set(courseInfo);
        return PREFIX + "courseInfo_edit.html";
    }

    /**
     * 获取直播课程列表
     */
	@Permission
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(value = "courseInfoName", required = false) String courseInfoName) {
    
    	Page page = LayuiPageFactory.defaultPage();
        //Page<Map<String, Object>> pageInfo = courseInfoService.pageMapsByParams(page, courseInfoName);
    	QueryWrapper<CourseInfo> queryWrapper = new QueryWrapper<CourseInfo>();
    	if(StrUtil.isNotEmpty(courseInfoName)){
            queryWrapper.and(wrapper -> wrapper.like("course_name", courseInfoName).or().like("course_address", courseInfoName).or().like("course_remark", courseInfoName));
        }
    	IPage<Map<String, Object>> pageInfo = courseInfoService.pageMaps(page, queryWrapper);
        return LayuiPageFactory.createPageInfo(pageInfo);
    }

    /**
     * 添加直播课程
     */
    @Permission
	@RequestMapping(value = "/add")
    @ResponseBody
    public Object add(CourseInfo courseInfo,HttpServletRequest request) {
        courseInfoService.save(courseInfo);
        String spreadUrl = request.getScheme()+ "://" +request.getServerName() + ":" + request.getServerPort()
                +"/wxauth/oncoSpread?courseId="+courseInfo.getCourseId();
        spreadUrl =spreadUrl.replace("47.104.108.189:8089","talkmed.ibiolink.com");
        courseInfo.setSpreadUrl(spreadUrl);
        courseInfoService.updateById(courseInfo);
        return BaseController.SUCCESS_TIP;
    }

    /**
     * 删除直播课程
     */
    @Permission
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long courseInfoId) {
        courseInfoService.removeById(courseInfoId);
        return BaseController.SUCCESS_TIP;
    }

    /**
     * 修改直播课程
     */
    @Permission
    @RequestMapping(value = "/edit")
    @ResponseBody
    public Object edit(CourseInfo courseInfo) {
        courseInfoService.updateById(courseInfo);
        return BaseController.SUCCESS_TIP;
    }

    /**
     * 直播课程详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail(@RequestParam("courseInfoId") Long courseInfoId) {
    	CourseInfo courseInfo = courseInfoService.getById(courseInfoId);
    	return ResponseData.success(courseInfo);
    }

    /**
     * HTOLOGY推送通知
     *
     * @param courseInfoId
     * @return
     */
//    @Permission
    @RequestMapping(value = "/sendNoticeHtology")
    @ResponseBody
    public Object sendNoticeHtology(@RequestParam("courseInfoId") Long courseInfoId) {
//        courseInfoService.sendNotice(courseInfoId, WXTypeEnums.HTOLOGY.getCode());
        return ResponseData.success();
    }

    /**
     *
     * ONCO推送通知
     *
     * @param courseInfoId
     * @return
     */
    @Permission
    @RequestMapping(value = "/sendNoticeOnco")
    @ResponseBody
    public Object sendNoticeOnco(@RequestParam("courseInfoId") Long courseInfoId) {
//        courseInfoService.sendNotice(courseInfoId,WXTypeEnums.ONCO.getCode());
        return ResponseData.success();
    }
}
